package com.pf.controller;

import com.google.common.collect.ImmutableMap;
import com.pf.config.RequestConfigHolder;
import com.pf.constants.SystemConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.config.RequestConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/custom/timeout")
public class TimeOutController {

    @Autowired
    @Qualifier("restTemplateCustomTimeOut")
    private RestTemplate restTemplateCustomTimeOut;

    @GetMapping("/test")
    public String test() {

        String url = "http://localhost:8081/timeout/get";
        Map<String, String> reqParams = ImmutableMap.of("requestId", "111", "accessToken", "222", "userId", "1001");
        url = buildReqParamsStr(url, reqParams);
        ResponseEntity<String> responseEntity = restTemplateCustomTimeOut.getForEntity(url, String.class);
        if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
            log.info(">>>>调用成功！");
            return responseEntity.getBody();
        } else {
            log.info(">>>>调用失败！");
            return "fail";
        }
    }


    @GetMapping("/test2")
    public String test2(Integer timeout) {

        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).build();
        RequestConfigHolder.put(requestConfig);
        String url = "http://localhost:8081/timeout/get";
        Map<String, String> reqParams = ImmutableMap.of("requestId", "111", "accessToken", "222", "userId", "1001");
        url = buildReqParamsStr(url, reqParams);
        ResponseEntity<String> responseEntity = restTemplateCustomTimeOut.getForEntity(url, String.class);
        if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
            log.info(">>>>调用成功！");
            return "ok";
        } else {
            log.info(">>>>调用失败！");
            return "fail";
        }
    }

    private String buildReqParamsStr(String url, Map<String, String> reqParams) {
        String reqParamsStr = "";
        if (reqParams != null && reqParams.size() > 0) {
            Set<String> keys = reqParams.keySet();
            for (String key : keys) {
                reqParamsStr = reqParamsStr + key + "=" + reqParams.get(key) + "&";
            }
            reqParamsStr = reqParamsStr.substring(0, reqParamsStr.length() - 1);
        }
        if (StringUtils.isNotBlank(reqParamsStr)) {
            url = url + SystemConstants.QUESTION_MARK + reqParamsStr;
        }
        return url;
    }
}
